﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AgileSaude.Data.Repository
{
    public class clinicaRepository : IclinicaRepository
    {
        agilesaudeEntities entity;

        public clinicaRepository()
        {
            entity = agilesaudeContext.getInstance();
        }

        public IQueryable<clinica> Getclinicas()
        {
            return agilesaudeContext.getInstance().clinica;
        }
        
        public IList<clinica> GetAll()
        {
            return agilesaudeContext.getInstance().clinica.ToList();
        }

        public clinica getclinica(int pclinicaId)
        {
            return entity.clinica.SingleOrDefault(a => a.idClinica == pclinicaId);
        }

        public void Delete(clinica pclinica)
        {
            try
            {
                entity.clinica.DeleteObject(pclinica);
                entity.SaveChanges();
            }
            catch (Exception ex)
            {
                agilesaudeContext.removeInstance();
                throw ex;
            }
        }

        public void Add(clinica pclinica)
        {
            try
            {
                entity.AddToclinica(pclinica);
                entity.SaveChanges();
            }
            catch (Exception ex)
            {
                agilesaudeContext.removeInstance();
                throw ex;
            }
        }

        public void Update()
        {
            try
            {
                entity.SaveChanges();
            }
            catch (Exception ex)
            {
                agilesaudeContext.removeInstance();
                throw ex;
            }
        }

        public clinica getclinica(string pNome)
        {
            return entity.clinica.SingleOrDefault(a => a.Nome == pNome);
        }
    }
}
